package com.deparse.dpframe.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.RelativeLayout;

import com.deparse.dpframe.R;

/**
 * Created by MartinDKent on 2016/1/16.
 */
public abstract class DPBaseActivityWithSimpleActionBar extends DPBaseActivity {
    private View contentView;

    private RelativeLayout headerContainer;
    private RelativeLayout bodyContainer;

    private TextView titleTextView;
    private ImageButton leftButton;
    private ImageButton rightButton;
    private TextView leftButtonTextView;
    private TextView rightButtonTextView;

    private RelativeLayout leftButtonContainer;
    private RelativeLayout rightButtonContainer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = inflate(R.layout.dp_base_activity_with_simple_actionbar);
        super.setContentView(contentView);

        headerContainer = (RelativeLayout) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_page_header);
        bodyContainer = (RelativeLayout) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_page_body);

        titleTextView = (TextView) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_page_title);
        leftButton = (ImageButton) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_left_button);
        rightButton = (ImageButton) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_right_button);

        leftButtonContainer = (RelativeLayout) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_left_button_container);
        rightButtonContainer = (RelativeLayout) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_right_button_container);

        leftButtonTextView = (TextView) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_left_button_text);
        rightButtonTextView = (TextView) contentView.findViewById(R.id.dp_base_activity_with_simple_actionbar_right_button_text);

        leftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!onActionBarLeftButtonClicked(leftButton)) {
                    finish();
                }
            }
        });

        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!onActionBarRightButtonClicked(rightButton)) {
                    //do nothing
                }
            }
        });

        if (0 != getActionBarBackgroundResource()) {
            headerContainer.setBackgroundResource(getActionBarBackgroundResource());
        }

        if (0 != getActionBarLayout()) {
            headerContainer.removeAllViewsInLayout();
            headerContainer.addView(inflate(getActionBarLayout()), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        } else {
            if (0 != getTitleResId()) {
                setTitle(getTitleResId());
            }
            if (0 != getLeftbuttonTextResId()) {
                setLeftButtonText(getLeftbuttonTextResId());
            }
            if (0 != getRightbuttonTextResId()) {
                setRightButtonText(getRightbuttonTextResId());
            }
            if (0 != getLeftbuttonIconResId()) {
                setLeftButtonIcon(getLeftbuttonIconResId());
            }
            if (0 != getRightbuttonIconResId()) {
                setRightButtonIcon(getRightbuttonIconResId());
            }
            if (0 != getLeftbuttonBackgroundResource()) {
                setLeftBackgroundResource(getLeftbuttonBackgroundResource());
            }
            if (0 != getRightbuttonBackgroundResource()) {
                setRightBackgroundResource(getRightbuttonBackgroundResource());
            }

            if (0 == getLeftbuttonIconResId() && 0 == getLeftbuttonTextResId()) {
                setLeftButtonVisiblity(View.GONE);
            } else {
                setLeftButtonVisiblity(View.VISIBLE);
            }
            if (0 == getRightbuttonIconResId() && 0 == getRightbuttonTextResId()) {
                setRightButtonVisiblity(View.GONE);
            } else {
                setRightButtonVisiblity(View.VISIBLE);
            }
        }

        if (0 != getContentViewLayout()) {
            bodyContainer.removeAllViewsInLayout();
            bodyContainer.addView(inflate(getContentViewLayout()), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        } else {
            //do nothing.
        }
    }

    public ViewGroup getActionBarContainer(){
        return headerContainer;
    }

    public void addViewInHeader(View view, RelativeLayout.LayoutParams params) {
        headerContainer.addView(view, params);
    }

    public int getActionBarBackgroundResource() {
        return 0;
    }

    public int getTitleResId() {
        return 0;
    }

    public int getLeftbuttonTextResId() {
        return 0;
    }

    public int getRightbuttonTextResId() {
        return 0;
    }

    public int getLeftbuttonIconResId() {
        return R.mipmap.dp_pat_ic_arrow_back_22x39;
    }

    public int getRightbuttonIconResId() {
        return 0;
    }

    public int getLeftbuttonBackgroundResource() {
        return 0;
    }

    public int getRightbuttonBackgroundResource() {
        return 0;
    }

    public int getActionBarLayout() {
        return 0;
    }

    public int getContentViewLayout() {
        return 0;
    }

    public boolean onActionBarLeftButtonClicked(ImageButton leftButton) {
        return false;
    }

    public boolean onActionBarRightButtonClicked(ImageButton rightButton) {
        return false;
    }

    public void setActionBar(View actionBar) {
        headerContainer.removeAllViewsInLayout();
        headerContainer.addView(actionBar, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public void setActionBarVisibility(int visibility) {
        headerContainer.setVisibility(visibility);
    }

    public void setActionBarBackgroundResource(int resId) {
        headerContainer.setBackgroundResource(resId);
    }

    public void setActionBarBackgroundColor(int color_action_bar_bg) {
        headerContainer.setBackgroundColor(color_action_bar_bg);
    }

    public void setTitleTextView(String title) {
        this.titleTextView.setText(title);
    }

    public void setTitle(int titleResId) {
        this.titleTextView.setText(titleResId);
    }

    public void setTitle(String title) {
        this.titleTextView.setText(title);
    }

    public void setTitleTextColor(int colorResId) {
        titleTextView.setTextColor(getResources().getColor(colorResId));
    }

    public void setTitleTextSize(int sizeInSp) {
        titleTextView.setTextSize(sizeInSp);
    }

    public void setTitleTextAppearance(int resid) {
        //noinspection deprecation
        titleTextView.setTextAppearance(this, resid);
    }

    public void setTitleTextSize(float sizeInSp) {
        titleTextView.setTextSize(sizeInSp);
    }

    public void setLeftButtonText(String buttonText) {
        this.leftButtonTextView.setText(buttonText);
    }

    public void setLeftButtonText(int buttonTextResId) {
        this.leftButtonTextView.setText(buttonTextResId);
    }

    public void setLeftButtonTextColor(int colorResId) {
        this.leftButtonTextView.setTextColor(getResources().getColor(colorResId));
    }

    public void setLeftButtonTextSize(int sizeInSp) {
        this.leftButtonTextView.setTextSize(sizeInSp);
    }

    public void setLeftButtonTextSize(float sizeInSp) {
        this.leftButtonTextView.setTextSize(sizeInSp);
    }

    public void setRightButtonText(String buttonText) {
        this.rightButtonTextView.setText(buttonText);
    }

    public void setRightButtonText(int buttonTextResId) {
        this.rightButtonTextView.setText(buttonTextResId);
    }

    public void setRightButtonTextColor(int colorResId) {
        this.rightButtonTextView.setTextColor(getResources().getColor(colorResId));
    }

    public void setRightButtonTextSize(int sizeInSp) {
        this.rightButtonTextView.setTextSize(sizeInSp);
    }

    public void setRightButtonTextSize(float sizeInSp) {
        this.rightButtonTextView.setTextSize(sizeInSp);
    }

    public void setLeftButtonIcon(int iconResId) {
        this.leftButton.setImageResource(iconResId);
    }

    public void setLeftBackgroundResource(int backgroundResId) {
        this.leftButton.setBackgroundResource(backgroundResId);
    }

    public void setRightButtonIcon(int iconResId) {
        this.rightButton.setImageResource(iconResId);
    }

    public void setRightBackgroundResource(int backgroundResId) {
        this.rightButton.setBackgroundResource(backgroundResId);
    }

    public void setLeftButtonVisiblity(int visiblity) {
        this.leftButtonContainer.setVisibility(visiblity);
    }

    public void setRightButtonVisiblity(int visiblity) {
        this.rightButtonContainer.setVisibility(visiblity);
    }

    /**
     * addContentView
     *
     * @param view
     * @param params
     */
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        bodyContainer.addView(view, params);
    }

    /**
     * addContentView
     *
     * @param view
     * @param index
     * @param params
     */
    public void addContentView(View view, int index, ViewGroup.LayoutParams params) {
        bodyContainer.addView(view, index, params);
    }

    public void addContentView(View view) {
        bodyContainer.removeView(view);
    }

    public void addContentView(int index) {
        bodyContainer.removeViewAt(index);
    }

    public View getContentView() {
        return bodyContainer;
    }

    @Override
    public void setContentView(int layoutResID) {
        bodyContainer.removeAllViewsInLayout();
        bodyContainer.addView(inflate(layoutResID), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void setContentView(View view) {
        bodyContainer.removeAllViewsInLayout();
        bodyContainer.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        bodyContainer.removeAllViewsInLayout();
        if (null == params) {
            params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        bodyContainer.addView(view, params);
    }
}
